define(['jquery', 'bootstrap', 'backend', 'table', 'form', '/assets/addons/kuerp/layui/layui.js'], function ($, undefined, Backend, Table, Form, Layui) {

    var Controller = {
        index: function () {

            layui.use(['table','upload','form'], function(){
                var table = layui.table;
                upload = layui.upload;

                // var serialList = Config.serialList;
                // var serial = xmSelect.render({
                //     el: '#serial',
                //     language: 'zn',
                //     tips: '序列号',
                //     paging: true,
                //     pageSize: 5,
                //     filterable: true,
                //     data: serialList
                // });

                var goodsList = Config.goodsList;
                var goods = xmSelect.render({
                    el: '#goods',
                    language: 'zn',
                    tips: '商品',
                    paging: true,
                    pageSize: 5,
                    filterable: true,
                    data: goodsList
                });

                table.render({
                    elem: '#table'
                    ,url:'kuerp/goods_serial_status/index'
                    ,parseData: function(res){ //res 即为原始返回的数据
                        return {
                            "code": 0, //解析接口状态
                            "msg": '', //解析提示文本
                            "count": res.total, //解析数据长度
                            "data": res.rows //解析数据列表
                        };
                    }
                    ,toolbar: '#thisToolbar' //开启头部工具栏，并为其绑定左侧模板
                    ,height: 'full-180'
                    ,page: true
                    ,limit: 50
                    ,limits: [10,20,30,50,100,150, 200]
                    ,cellMinWidth: 100 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                    ,cols: [[
                        {checkbox: true},
                        // {field: 'id', title: __('ID')},
                        {field: 'goods_code', title: __('商品编号')},
                        {field: 'goods_name', title: __('商品名称')},
                        {field: 'goods_spec', title: __('规格型号'),hide:true},
                        {field: 'serial', title: __('Serial'),minWidth:150},
                        {field: 'remark', title: __('锁内码'),minWidth:150},
                        {field: 'stores_name', title: __('仓库')},
                        {field: 'updatetime', title: __('最后业务日期'),templet: function (d) {
                                if(d.updatetime!=null){
                                    return layui.util.toDateString(d.updatetime*1000,"yyyy-MM-dd");
                                }else{
                                    return '';
                                }
                            },minWidth:150},
                        {field: 'createtime', title: __('入库日期'),templet: function (d) {
                                if(d.createtime!=null){
                                    return layui.util.toDateString(d.createtime*1000,"yyyy-MM-dd");
                                }else{
                                    return '';
                                }
                            },minWidth:150,hide:true},
                        {field: 'status_text', title: __('Status')},
                        {field: 'customer_name', title: __('客户/供应商'),minWidth:150,templet: function (d) {
                            if (d.customer_name != '') {
                                return "<a href='javascript:void(0);' style='color: #4397fd' class='btn-dialog' " +
                                "data-url='kuerp/goods_sales/detail/ids/"+d.goods_salesid+"' data-title='订单详情' data-area=[\"80%\",\"80%\"] >" + d.customer_name + "</a>";
                            } else {
                                return '';
                            }
                            
                        }},
                        {field: 'store_status_text', title: __('Store_status')},
                        {field: 'operate', title:__('Operate'), toolbar: '#toolDemo', width:150},
                    ]]
                });

                upload.render({
                    elem: '#import'
                    ,url: 'ajax/upload'
                    ,done: function(res, index, upload){ //上传后的回调
                        if(res.code==1){
                            Fast.api.ajax("kuerp/goods_serial_status/import?file="+res.data.url, "",'',true);
                        }else{
                            Toastr.error(res.msg);
                        }
                    }
                    ,accept: 'file' //允许上传的文件类型
                    ,size: 50 //最大允许上传的文件大小
                });

                layui.form.on('switch(switchList)', function (obj) {
                    var onoff = this.checked ? '1' : '0';
                    var id = $(this).data('id');
                    $.ajax( {
                        url: "kuerp/goods_serial_status/multi",
                        type: "POST",
                        data:{
                            'ids':id,
                            'params':"status="+onoff,
                        },
                        success: function(res){
                            if(res.code==0){
                                Toastr.error(res.msg);
                            }else{
                                Toastr.success('操作成功');
                            }
                        }
                    });
                });

                //头工具栏事件
                table.on('toolbar(table)', function(obj){
                    var checkStatus = table.checkStatus(obj.config.id);
                    switch(obj.event){
                        case 'refresh':
                            table.reload('table', []);
                            break;
                        case 'add':
                            Fast.api.open("kuerp/goods_serial_status/add", "添加", {
                                callback:function(value){
                                    Controller.api.bindevent();
                                }
                            });
                            break;
                        case 'delAll':
                            var data = checkStatus.data;
                            var ids = '';
                            $(data).each(function (k,v) {
                                ids+=v.id+',';
                            });
                            Fast.api.ajax("kuerp/goods_serial_status/del?ids="+ids, "",'',true);
                            break;
                    }
                });

                //监听行工具事件
                table.on('tool(table)', function(obj){
                    var data = obj.data;
                    if(obj.event === 'del'){
                        $.ajax( {
                            url: "kuerp/goods_serial_status/del",
                            type: "POST",
                            data:{'ids':data.id},
                            success: function(res){
                                if(res.code==1){
                                    Toastr.success('操作成功');
                                    setTimeout(function () {
                                        parent.location.reload();
                                    },1500)
                                }else{
                                    Toastr.error(res.msg);
                                }
                            }
                        });
                    } else if(obj.event === 'edit'){
                        Fast.api.open("kuerp/goods_serial_status/edit?ids="+data.id, "编辑");
                    }else if(obj.event === 'outbound'){
                        Fast.api.open("kuerp/goods_serial_status/outbound?ids="+data.id, "出库");
                    }
                });
                //搜索事件
                $('#search').on('click', function () {
                    // var serialArr = serial.getValue();
                    // var serial_id = '';
                    // if(serialArr.length>0){
                    //     $(serialArr).each(function (k,v) {
                    //         if(serial_id===''){
                    //             serial_id += v.value;
                    //         }else{
                    //             serial_id += ','+v.value;
                    //         }
                    //     })
                    // }
                    var goodsArr = goods.getValue();
                    var goods_id = '';
                    if(goodsArr.length>0){
                        $(goodsArr).each(function (k,v) {
                            if(goods_id===''){
                                goods_id += v.value;
                            }else{
                                goods_id += ','+v.value;
                            }
                        })
                    }
                    var search = {};
                    var serial = $('#serial').val();
                    if(serial!=''){
                        search.serial = serial;
                    }
                    var remark = $('#remark').val();
                    if(remark!=''){
                        search.remark = remark;
                    }
                    if(goods_id!=''){
                        search.goods_id = goods_id;
                    }
                    var stores_id = $('#stores_id').val();
                    if(stores_id!=''){
                        search.stores_id = stores_id;
                    }
                    var status = $('#status').val();
                    if(status!=''){
                        search.status = status;
                    }
                    var store_status = $('#store_status').val();
                    if(store_status!=''){
                        search.store_status = store_status;
                    }
                    var updatetime = $('#updatetime').val();
                    if(updatetime!=''){
                        search.updatetime = updatetime;
                    }
                    table.reload('table', {
                        where: {
                            filter:JSON.stringify(search),
                            op:JSON.stringify({'goods_id':'IN','stores_id':'IN','updatetime':'RANGE', 'serial':'LIKE', 'remark':'LIKE'}),
                        }
                        ,page: {
                            curr: 1
                        }
                    });
                });
            });
            Controller.api.bindevent();
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        outbound: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                layui.use('laydate', function(){
                    var laydate = layui.laydate;
                    //执行一个laydate实例

                    laydate.render({
                        elem: '#c-purchase_time',
                        type: 'datetime'
                    });
                    laydate.render({
                        elem: '#updatetime',
                        type: 'datetime',
                        range: true
                    });
                });
                $(document).on("click", ".select-contract", function(){
                    var $this=$(this)
                    top.Fast.api.open($(this).data("url") , __('Select'), {callback: function (data) {
                        $this.html(data.name);
                        $('[name="'+$this.data('name')+'"]').val(data.id);
                        $('#customer_name').html('关联客户：'+data.customer_name).removeClass('hide');
                    },area:["60%", "60%"]});
                });
                Form.api.bindevent($("form[role=form]"), function(data, ret){
                    parent.location.reload();
                });
            }
        }
    };
    return Controller;
});